package com.casstime.ec.cloud.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class MybatisConfig extends GenericMybatisConfig {

  @Bean(name = "defaultTransactionManager")
  public PlatformTransactionManager defaultTransactionManager(DataSource dataSource) {
    DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
    transactionManager.setGlobalRollbackOnParticipationFailure(false);
    return transactionManager;
  }

  @Bean
  public SqlSessionFactory getSqlSessionFactoryBean(DataSource dataSource) throws Exception {
    SqlSessionFactoryBean sqlSessionFactoryBean = super.getSqlSessionFactoryBean(dataSource,
        null,
        "classpath:com/casstime/ec/cloud/sprintlog/dao/*.xml");
    return sqlSessionFactoryBean.getObject();
  }

  @Bean
  public MapperScannerConfigurer getMapperScannerConfigurer() {
    return super.getMapperScannerConfigurer("getSqlSessionFactoryBean",
        "com.casstime.ec.cloud.sprintlog.dao");
  }

}
